package main

import "fmt"

func test1() {
	var number = make([]int, 3, 5)
	printSlice(number)
}

func test2() {
	var numbers []int
	printSlice(numbers)

	if numbers == nil {
		fmt.Println("切片是空的")
	}
}

func test3() {
	numbers := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
	printSlice(numbers)
	fmt.Println("numbers ==", numbers)

	fmt.Println("numbers[1:4] == ", numbers[1:4])

	fmt.Println(numbers[:3])
	fmt.Println("numbers[4:] ==", numbers[4:])

	numbers1 := make([]int, 0, 5)
	printSlice(numbers1)

	number2 := numbers[:2]
	printSlice(number2)

}

func test4() {
	var numbers []int
	printSlice(numbers)

	numbers = append(numbers, 0)
	printSlice(numbers)

	numbers = append(numbers, 1)
	printSlice(numbers)

	numbers = append(numbers, 2, 3, 4)
	printSlice(numbers)
	numbers1 := make([]int, len(numbers), (cap(numbers))*2)
	copy(numbers1, numbers)
	printSlice(numbers1)

}

func printSlice(x []int) {
	fmt.Printf("len=%d cap=%d slice=%v\n", len(x), cap(x), x)
}

func main() {
	//test1()
	//test2()
	//test3()
	test4()
}
